﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JumJob.Models;
namespace JumJob.Controllers
{
    public class BidController : Controller
    {
           jum_jobEntities jumDb;
        //
        // GET: /Job/

        public BidController() 
        {
            
            jumDb = new jum_jobEntities();
        }
        //
        // GET: /Bid/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Bid/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Bid/Create
        [Authorize]
        public ActionResult Create()
        {
            ViewData["job_id"] = Request.QueryString["job_id"];
            return View();
        } 

        public ActionResult MyBids()
        {
            List<Bid> bids = (from bid in jumDb.bid
                              where bid.posted_by == User.Identity.Name
                              select bid).ToList();
                          
            return View(bids);
        }

        public ActionResult Posted() 
        {
            
            return View();
        }

        public ActionResult Accept(int id) 
        {
            Bid bid = (from bidi in jumDb.bid
                       where bidi.id == id
                       select bidi).FirstOrDefault();
            bid.status = BidStatus.ACEPTADA;
            bid.jum_job.status = JobStatus.EN_PROCESO;
            jumDb.SaveChanges();
            return View();
        }

        //
        // POST: /Bid/Create
        [HttpPost]
        public ActionResult Create(Bid bid)
        {
            if (ModelState.IsValid) 
            {
                bid.posted_by = User.Identity.Name;
                bid.status = BidStatus.SOLICITADA;
                jumDb.bid.AddObject(bid);
                jumDb.SaveChanges();
                return RedirectToAction("Posted");
            }
            return View();       
        }
        
        //
        // GET: /Bid/Edit/5

 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Bid/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Bid/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Bid/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
